package com.aq.appserver.server.user.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.aq.appserver.server.user.bean.Menu;
import com.aq.appserver.server.user.bean.UserBean;
import com.aq.appserver.server.user.entity.User;
import com.aq.appserver.server.user.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController{

	@Autowired
	private UserService userService;
	
	@RequestMapping("/login")
	@ResponseBody
	public Object login(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request) {
		Map<String, Object> response = new HashMap<String, Object>();
		
		// 登录成功falg=0，失败flag=1，msg为错误信息,验证码：(String)session.getAttribute("validateCode")
		userService.validateLogin(userBean, response, request);
		
		// 查询用户菜单
		Menu m1 = new Menu();
		m1.setId("11");
		m1.setLevel("1");
		m1.setName("业务管理");
		m1.setParentID("-1");
		m1.setUrl("");
		m1.setHasLevel3("0");
		
		Menu m2 = new Menu();
		m2.setId("22");
		m2.setLevel("1");
		m2.setName("App用戶管理");
		m2.setParentID("-1");
		m2.setUrl("");
		m2.setHasLevel3("0");
		
		Menu m3 = new Menu();
		m3.setId("33");
		m3.setLevel("1");
		m3.setName("系统管理");
		m3.setParentID("-1");
		m3.setUrl("");
		m3.setHasLevel3("0");
		
		Menu m11 = new Menu();
		m11.setId("1101");
		m11.setLevel("2");
		m11.setName("主题管理");
		m11.setParentID("11");
		m11.setUrl("");
		m11.setHasLevel3("1");
		
		Menu m111 = new Menu();
		m111.setId("110101");
		m111.setLevel("3");
		m111.setName("主题管理");
		m111.setParentID("1101");
		m111.setUrl("pages/business/theme.jsp");
		m111.setHasLevel3("0");
		
		Menu m21 = new Menu();
		m21.setId("2201");
		m21.setLevel("2");
		m21.setName("App用戶管理");
		m21.setParentID("22");
		m21.setUrl("pages/account/account.jsp");
		m21.setHasLevel3("0");
		
		Menu m31 = new Menu();
		m31.setId("3301");
		m31.setLevel("2");
		m31.setName("用户管理");
		m31.setParentID("33");
		m31.setUrl("pages/system/user.jsp");
		m31.setHasLevel3("0");
		
		List<Menu> menuList = new ArrayList<Menu>();
		menuList.add(m1);
		menuList.add(m2);
		menuList.add(m11);
		menuList.add(m111);
		menuList.add(m21);
		if("admin".equals(userBean.getUserName()))
		{
			menuList.add(m3);
			menuList.add(m31);
		}
		
		userBean.setMenuJson(JSONArray.toJSONString(menuList));
		
		userBean.setDefaultMenuID1("11");
		
		userBean.setDefaultMenuID23("110101");
		
		userBean.setDefaultMenuUrl("pages/business/ask.jsp");
		
		userBean.setUserName("admin");

		request.getSession().setAttribute("userSessionInfo", userBean);
		return response;
	}
	
	@RequestMapping("/queryUserList")
	@ResponseBody
	public Object queryUserList(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request) {
		Map<String, Object> response = new HashMap<String, Object>();
		List<UserBean> list = new ArrayList<UserBean>();
		for(User u : userService.queryAll())
		{
			UserBean b = new UserBean();
			b.setUserID(u.getId() + "");
			b.setUserCode(u.getUserCode());
			b.setUserName(u.getUserName());
			b.setPassword(u.getPassword());
			if("admin".equals(u.getUserName()))
			{
				b.setRoleName("管理员");
			}
			else
			{
				b.setRoleName("普通用户");
			}
			list.add(b);
		}
		response.put("rows", list);
		response.put("total", list.size());
		return response;
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public Object updateUser(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request) {
		Map<String, Object> response = new HashMap<String, Object>();
		User user = new User();
		transBeanToEntity(userBean, user);
		userService.update(user);
		return response;
	}
	
	@RequestMapping("/saveUser")
	@ResponseBody
	public Object saveUser(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request) {
		Map<String, Object> response = new HashMap<String, Object>();
		User user = new User();
		transBeanToEntity(userBean, user);
		userService.save(user);
		response.put("flag", "0");
		return response;
	}
	
	@RequestMapping("/deleteUser")
	@ResponseBody
	public Object deleteUser(@ModelAttribute("userBean") UserBean userBean, HttpServletRequest request) {
		Map<String, Object> response = new HashMap<String, Object>();
		userService.delete(userBean);
		response.put("flag", "0");
		return response;
	}
	
	public void transBeanToEntity(UserBean bean, User entity)
	{
		if(bean.getUserID()!=null)
		{
			entity.setId(Long.parseLong(bean.getUserID()));
		}
		entity.setPassword(bean.getPassword());
		entity.setUserCode(bean.getUserCode());
		entity.setUserName(bean.getUserName());
	}
	
	public static void main(String[] args) {
	}
}
